﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace TPAMain工序结余锁定工具
{
    class LockHelper
    {
        public static bool DoLock(string strLockMode, string strKey, string strOperator, string strLockReason,DataTable tmpdt)
        {
            SqlConnection conn=new SqlConnection(GeneralTools.GetConnectionString("DefualtDataBase"));
            conn.Open();
            SqlTransaction tran = conn.BeginTransaction();
            try
            {
                #region 写入主记录
                string strInsertString = "insert into LockMainLog (LockMode,LockKey,LockReason,LockOperator,LockOperateTime) values ('@锁定模式','@关键词','@锁定原因','@锁定人',GetDate())";
                SqlCommand cmd = new SqlCommand(strInsertString.Replace("@锁定模式", strLockMode)
                                                                                                    .Replace("@关键词", strKey)
                                                                                                    .Replace("@锁定原因", strLockReason)
                                                                                                    .Replace("@锁定人", strOperator), conn);
                cmd.Transaction = tran;
                cmd.ExecuteNonQuery();
                #endregion
                #region 获取主记录ID
                cmd.CommandText = "select scope_identity()";
                int iMainID=GeneralTools.GetInt(cmd.ExecuteScalar (), -1);
                if (iMainID <0)
                {
                    throw new Exception("写入主记录失败（获取主记录ID失败）！");
                }
                #endregion
                #region 写入明细记录
                strInsertString = "insert into LockDetailLog (MainID,GxjyID,OriginalGxjy,OriginalBz,SN,ChipSN,GDNo) values (@MainID,@GxjyID,@OriginalGxjy,'@OriginalBz','@SN','@ChipSN','@GDNo')";
                string strUpdateString = "update TPAMain.dbo.gxjy_table set gxjy008=0,gxjy015='@Bz',Locked=1 where gxjy000=@GxjyID";
                for(int i=0;i<tmpdt .Rows.Count;i++)
                {
                    //写入明细记录
                    cmd.CommandText = strInsertString.Replace("@MainID", iMainID.ToString())
                                                                            .Replace("@GxjyID", tmpdt.Rows [i]["工序结余ID"].ToString ())
                                                                            .Replace("@OriginalGxjy", tmpdt.Rows[i]["原始工序结余"].ToString())
                                                                            .Replace("@OriginalBz", tmpdt.Rows[i]["原始工序结余备注"].ToString())
                                                                            .Replace("@GDNo", tmpdt.Rows[i]["工单号"].ToString())
                                                                            .Replace("@SN", tmpdt.Rows[i]["批号"].ToString())
                                                                            .Replace("@ChipSN", tmpdt.Rows[i]["芯片号"].ToString());
                    cmd.ExecuteNonQuery();
                    //更新工序结余记录
                    cmd.CommandText = strUpdateString.Replace("@Bz", "结余已锁定：" + strLockReason)
                                                                                  .Replace("@GxjyID", tmpdt.Rows[i]["工序结余ID"].ToString());
                    cmd.ExecuteNonQuery();
                }
                #endregion
                #region 提交事务
                tran.Commit();
                #endregion 
                return true;
            }
            catch (Exception ex)
            {
                #region 回滚事务
                tran.Rollback();
                #endregion
                GeneralTools.ShowErrorMsg("写入锁定数据错误："+ex.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }
        }

        public static bool DoUnlock(string strOperator, string strUnlockReason, DataTable tmpdt)
        {
            SqlConnection conn = new SqlConnection(GeneralTools.GetConnectionString("DefualtDataBase"));
            conn.Open();
            SqlTransaction tran = conn.BeginTransaction();
            try
            {
                string strUpdateLog = "update LockDetailLog set Unlock=1,UnlockOperator='@解锁人',UnlockReason='@解锁原因',UnlockTime=GetDate() where ID='@锁定明细ID'";
                string strUpdateGxjy = "update TPAMain.dbo.gxjy_table set gxjy008=@工序结余,gxjy015='@备注',Locked=0 where gxjy000=@结余ID";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Transaction = tran;
                #region 更新记录
                for(int i=0;i<tmpdt.Rows.Count;i++)
                {
                    //更新日志
                    cmd.CommandText  = strUpdateLog.Replace("@解锁人", strOperator )
                                                                            .Replace("@解锁原因", strUnlockReason )
                                                                            .Replace("@锁定明细ID", tmpdt.Rows [i]["解锁明细ID"].ToString ());
                    cmd.ExecuteNonQuery();
                    //更新工序结余
                    cmd.CommandText =strUpdateGxjy.Replace("@工序结余", tmpdt.Rows[i]["原工序结余"].ToString())
                                                                            .Replace("@备注", tmpdt.Rows[i]["原备注"].ToString())
                                                                            .Replace("@结余ID", tmpdt.Rows[i]["工序结余ID"].ToString());
                    cmd.ExecuteNonQuery();
                }
                #endregion
                #region 提交事务
                tran.Commit();
                #endregion 
                return true;
            }
            catch (Exception ex)
            {
                #region 回滚事务
                tran.Rollback();
                #endregion
                GeneralTools.ShowErrorMsg("写入解锁数据错误：" + ex.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
